import requests
from lxml import etree
from modules.utils.log_util import LOGGER

"""
抓取360热搜
"""

baseurl = "https://news.so.com/hotnews?src=hotnews"
header = {
    "referer": "https://www.so.com/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
}


def get_q360_data():
    data = []
    LOGGER.info("请求360热搜数据")
    try:
        response = requests.get(baseurl, timeout=10, headers=header)
        html = response.text
    except Exception as e:
        LOGGER.error(e)
    else:
        try:
            tree = etree.HTML(html)
            list_node = tree.xpath("//div[@id='hot-list']//ul//a")
            for node in list_node:
                title = node.xpath("./span[@class='title']")[0].text
                hot_index = node.xpath("./span[@class='hot']")[0].text.strip("人在看")
                url = node.xpath("./@href")[-1]
                item = {"title": title, "hot_index": hot_index, "url": url}
                data.append(item)
        except Exception as e:
            LOGGER.error(e)
    return data


if __name__ == "__main__":
    print(get_q360_data())
